/**
 *  *2010-9-8 下午06:00:02  add 
 */
package com.base.myproject.client.busi.ccode;

import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * @author Administrator 2010-9-8 javazgw@gmail.com
 */
public class DeleteCcode_s extends Dialog implements Listener<BaseEvent> {
	FormPanel w;
	TextField<String> cardno_t;
	HorizontalPanel h = new HorizontalPanel();
	public DeleteCcode_s() {
		init();
	}

	private void init() {

	}

	protected void onRender(Element parent, int index) { 

		super.onRender(parent, index);
		initmainpanel();
		setButtons(Dialog.YESNO);
		getButtonById(Dialog.YES).addListener(Events.Select, this);
		getButtonById(Dialog.NO).addListener(Events.Select, this);
		getButtonById(Dialog.YES).setText("删除");
		getButtonById(Dialog.NO).setText("取消");
		getButtonById(Dialog.YES).setIcon(
				AbstractImagePrototype.create(BaseResource.INSTANCE
						.deleterecord()));
		setHeading("删除会员");
		setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
				.deleterecord()));
//
//		Label  cardno_l = new Label("需要删除会员卡号");
//		h.setBorders(true);
//		h.add(cardno_l);
//		h.add(cardno_t);
//		h.setBorders(false);
//		add(h,new RowData(1, 0.2));
		add(w, new RowData(1, 0.8));
	}

	private void initmainpanel() {
		w = new FormPanel();
		w.setHeaderVisible(false);
		w.setBodyBorder(false);
		cardno_t = new TextField<String>();
		cardno_t.setAllowBlank(false);
		cardno_t.setFieldLabel("需要删除的会员卡号");
		w.setFieldWidth(100);
		w.setLabelWidth(120);
		Html html =new Html("操作此界面，你必须清楚你现在在做什么。这是不可逆的操作。此操作会删除该会员的<strong>消费、充值、会员帐户</strong>信息");
		w.add(html);
		w.add(cardno_t);
		w.setStyleAttribute("background-color", "red");

		
	}

	@Override
	public void handleEvent(BaseEvent be) {
		if (be.getType().equals(Events.Select)) {
			if (be.getSource().equals(getButtonById(Dialog.YES))) {
				if(w.isValid())
				{
				this.getButtonById(Dialog.YES).setEnabled(false);
			
				if(Window.confirm("你正在删除会员，卡号:"+cardno_t.getValue()+"确定要这么做吗？"))
				{
					deleteCcode();
				}
				else
				{
					this.getButtonById(Dialog.YES).setEnabled(true);
				}
				}
			} else if (be.getSource().equals(getButtonById(Dialog.NO))) {
				this.hide();
			}

		}
	}

	void deleteCcode() {
		if (cardno_t.getValue() != null) {
			String backpay = "insert into s_service_po_pay_bak (ccode,s_discount,g_discount,paid,upicode,yingshou,accountcode,pay_g,status,bakdate) select ccode,s_discount,g_discount,paid,upicode,yingshou,accountcode,pay_g,status,getdate() from s_service_po_pay where exists (select * from  ccode where ccode.ccode =s_service_po_pay.ccode and ccode.s_cardno = '"+cardno_t.getValue()+"' )";
			String deletepay = "delete s_service_po_pay  where exists (select * from  ccode where ccode.ccode =s_service_po_pay.ccode and ccode.s_cardno = '"+cardno_t.getValue()+"' )";
			String s_service_finance = "insert into  s_service_finance_bak (icode,ccode,finance,updatedate,dc,bcode,finance_real,finance_gif,accountcode,rbcode,isnew,bakdate ) select icode,ccode,finance,updatedate,dc,bcode,finance_real,finance_gif,accountcode,rbcode,isnew ,getdate() from s_service_finance where exists (select * from  ccode where ccode.ccode =s_service_finance.ccode and ccode.s_cardno = '"+cardno_t.getValue()+"' )";
			String deletes_service_financesql = "delete s_service_finance where exists (select * from  ccode where ccode.ccode =s_service_finance.ccode and ccode.s_cardno = '"+cardno_t.getValue()+"' )";
			String backccode = "insert into  ccodebak (icode,ccode,cname, ccodetype,s_cardno,s_sex,s_edu,s_birid,s_mobil,rbcode,bakdate ) select icode,ccode,cname, ccodetype,s_cardno,s_sex,s_edu,s_birid,s_mobil,rbcode,getdate() as bakdate  from ccode where s_cardno = '"+cardno_t.getValue()+"' ";
			String deleteccode = "delete ccode where s_cardno = '"+cardno_t.getValue()+"' ";
			
			
			CommandSyncsql csql = new CommandSyncsql();
			
			csql.getV().add(backpay);
			csql.getV().add(deletepay);
			csql.getV().add(s_service_finance);
			csql.getV().add(deletes_service_financesql);
			csql.getV().add(backccode);
			csql.getV().add(deleteccode);
			csql.setContinueorrollback(false);
			CommandSyncContainer list = new CommandSyncContainer();
			list.add(csql);

			GreetingServiceAsync greetingService = GWT
					.create(GreetingService.class);
			greetingService.SendCommandSync(list,
					new AsyncCallback<CommandSyncReturnObject>() {
						@Override
						public void onFailure(Throwable caught) {
							MessageBox.alert("错误", "网络连接失败", null);
							getButtonById(Dialog.YES).setEnabled(true);
						}

						@Override
						public void onSuccess(CommandSyncReturnObject result) {
							if (result.isB()) {
								Info.display("", "删除会员成功");
								getButtonById(Dialog.YES).setEnabled(true);
							} else {
								MessageBox.alert("错误", "错误，错误代码"
										+ result.getMessage(), null);
								getButtonById(Dialog.YES).setEnabled(true);
							}
						}
					});
		}
	}
}
